#ifndef MYMAP
#define MYMAP

#include "RBTree.h"

namespace wk
{
    template <class K, class V>
    class map
    {
        // 和set一样，也需要一个仿函数来获取key
        struct MapKeyOfT
        {
            const K &operator()(const std::pair<K, V> &kv)
            {
                return kv.first;
            }
        };

    private:
        using iterator = typename RBTree<const K, std::pair<const K, V>, MapKeyOfT>::Iterator;
        using const_iterator = typename RBTree<const K, std::pair<const K, V>, MapKeyOfT>::ConstIterator;

    public:
        std::pair<iterator, bool> insert(const std::pair<K, V> &data)
        {
            return _t._insert(data);
        }

        iterator begin()
        {
            return _t.Begin();
        }

        iterator end()
        {
            return _t.End();
        }

        const_iterator cbegin() const
        {
            return _t.Begin();
        }

        const_iterator cend() const
        {
            return _t.End();
        }

        V &operator[](const K &key)
        {
            std::pair<iterator, bool> ret = insert({key, V()});
            return ret.first->second;
        }

    private:
        // STL中set的key是不能被修改的，所以这了第二个模板参数给了const
        RBTree<const K, std::pair<const K, V>, MapKeyOfT> _t;
    };
}

#endif